package com.xm.adv.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.xm.adv.R;
import com.xm.adv.activity.InfoActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import xinya.com.baselibrary.router.RouterUrl;
import xinya.com.baselibrary.router.RouterUtil;


public class SetDialog extends Dialog {

    private Context context;
    private ViewHolder viewHolder;

    public SetDialog(Context context) {
        super(context, R.style.tranDialog);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        init();
    }

    public void init() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_set, null);
        setContentView(view);
        viewHolder = new ViewHolder(view);
        viewHolder.textName.postDelayed(() -> dismiss(), 5000);
        viewHolder.btnAppInfo.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setClass(getContext(), InfoActivity.class);
            getContext().startActivity(intent);
        });
        viewHolder.btnSetInfo.setOnClickListener(v ->
                RouterUtil.getPostcardWithTransition(getContext(), RouterUrl.Set.set_set)
                .navigation());
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.7); // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
    }


    @Override
    public boolean onGenericMotionEvent(MotionEvent event) {
        if (event.getButtonState() == 128) {
            dismiss();
        }
        return super.onGenericMotionEvent(event);
    }

    static
    class ViewHolder {
        @BindView(R.id.textName)
        TextView textName;
        @BindView(R.id.btnLoginMangager)
        Button btnLoginMangager;
        @BindView(R.id.btnSetInfo)
        Button btnSetInfo;
        @BindView(R.id.btnOutInfo)
        Button btnOutInfo;
        @BindView(R.id.btnAppInfo)
        Button btnAppInfo;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}